<template>
  <div class="l-rblock">
    <l-table
      ref="mainTable"
      :loading="tableLoading"
      :columns="columns"
      :dataSource="tableData"
      :isPage="true"
      :pageTotal="tableTotal"
      :tablePage.sync="tableCurrentPage"
      @loadPageData="turnTablePage"
    >
      <template v-slot:wf_f_UnitName="scope">
        <el-button @click="handleOpen(scope.row)" size="mini" type="text">{{
          scope.row.wf_f_UnitName
        }}</el-button>
      </template>

      <template v-slot:wf_f_IsUrge="scope">
        <el-tag v-if="scope.row.wf_f_IsUrge == 1" size="mini" type="warning"
          >{{$t('催办')}}</el-tag>
        <el-tag v-else size="mini" type="success">{{$t('正常')}}</el-tag>
      </template>
      <template v-slot:wf_f_CreateDate="scope">
        {{ lr_dateFormat(scope.row.wf_f_CreateDate) }}
      </template>
      <template v-slot:wf_f_IsFinished="scope">
        <el-tag v-if="scope.row.wf_f_EnabledMark == 3" size="mini" type="danger"
          >{{$t('作废')}}</el-tag
        >
        <el-tag
          v-else-if="scope.row.wf_f_IsFinished == 1"
          size="mini"
          type="warning"
          >{{$t('结束')}}</el-tag
        >
        <el-tag
          v-else-if="scope.row.wf_f_IsAgain == 1"
          size="mini"
          type="primary"
          >{{$t('重新提交')}}</el-tag
        >
        <el-tag v-else size="mini" type="success">{{$t('审批中')}}</el-tag>
      </template>
      <template v-for="item in formColumns" v-slot:[item.prop]="scope">
        <slot v-bind="scope" :name="item.prop"></slot>
      </template>

      <l-table-btns
        v-if="type == 'completed'"
        :isAuth="false"
        :filterBtns="filterBtns"
        :btns="tableBtns"
        @click="lr_handleTableBtnClick"
      ></l-table-btns>
    </l-table>

    <wf-audit-form
      v-if="type == 'uncompleted'"
      ref="form"
      type="audit"
      :isCreateAgain="isCreateAgain"
      :isCreateSub="isCreateSub"
      :params="{ taskId: taskId }"
      :isLoadFormData="true"
      :title="title"
      :visible.sync="visible"
      @refresh="loadTableData(true)"
    ></wf-audit-form>

    <wf-look-form
      v-if="type == 'my'"
      ref="form"
      type="lookmy"
      :params="{ processId: processId }"
      :isLoadFormData="true"
      :title="title"
      :visible.sync="visible"
    >
    </wf-look-form>

    <wf-look-form
      v-if="type == 'completed'"
      ref="form"
      type="look"
      :params="{ taskId: taskId }"
      :isLoadFormData="true"
      :title="title"
      :visible.sync="visible"
    >
    </wf-look-form>
  </div>
</template>
<script>
const api = window.$api.workflow.process;
import mixin from "../../mixins/wfIndex";
export default {
  name: "l-wf-audit-table",
  mixins: [mixin()],
  props: {
    code: String,
    columns: {
      type: Array,
      default: () => [],
    },
    type: {
      type: String,
      default: "uncompleted",
    }, // 组件显示数据类型 uncompleted 代码任务；my 我提交的任务；completed 已经完成任务
    whereSql: String,
    loadFormDataList: Function,
  },
  data() {
    return {
      tableLoading: false,
      tableData: [],
      tableTotal: 0,
      tableCurrentPage: 1,
      tablePageSize: 50,

      queryData: {},

      tableBtns: [{ prop: "Revoke", label: "撤销" }],
    };
  },
  computed: {
    formColumns() {
      return this.columns.filter((t) => t.prop.indexOf("wf_") == -1);
    },
  },
  methods: {
    filterBtns(row, btns) {
      if (
        row.wf_f_IsCancel == 1 &&
        row.wf_f_IsFinished != 1 &&
        row.wf_isDelete != 3
      ) {
        return btns;
      } else {
        return [];
      }
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTableData(true);
    },
    async loadTableData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      this.tableLoading = true;
      const queryData = this.$deepClone(this.queryData);
      queryData.code = this.code;

      const pagination = {
        rows: this.tablePageSize,
        page: this.tableCurrentPage,
      };

      if (this.whereSql) {
        queryData.where = this.lr_AESEncrypt(
          JSON.stringify(this.whereSql),
          "David"
        );
      }
      let data = null;

      if (this.type == "uncompleted") {
        pagination.sidx = "f_IsUrge DESC,t.F_CreateDate DESC";
        data = await this.$awaitWraper(
          api.getUnCompletedMyPage(pagination, queryData)
        );
      } else if (this.type == "my") {
        pagination.sidx = "F_CreateDate DESC";
        data = await this.$awaitWraper(api.getMyPage(pagination, queryData));
      } else if (this.type == "completed") {
        pagination.sidx = "F_CreateDate DESC";
        data = await this.$awaitWraper(
          api.getCompletedMyPage(pagination, queryData)
        );
      }

      if (data != null) {
        const idList = data.rows.map((t) => t.f_ProcessId || t.f_Id);

        if (this.loadFormDataList && data.rows.length > 0) {
          const formDataList = await this.loadFormDataList(String(idList));
          data.rows = this.mapData(data.rows, formDataList);
        } else {
          data.rows = this.mapData(data.rows, {});
        }

        this.tableData = data.rows;
        this.tableTotal = data.records;
      } else {
        this.tableData = [];
      }
      this.tableLoading = false;
    },

    mapData(rows, formDataList) {
      return rows.map((t) => {
        const res = this.$deepClone(formDataList[t.f_ProcessId || t.f_Id] || {});
        for (let key in t) {
          res[`wf_${key}`] = t[key];
        }
        return res;
      });
    },

    async loadData(queryData) {
      this.queryData = queryData || {};
      await this.loadTableData();
    },
  },
};
</script>